//
//  YMListViewModel.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/23.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

class YMListViewModel: NSObject {

    var status:YMStatus?
    lazy var statusList:[YMStatusViewModel] = [YMStatusViewModel]()
    
    
    
    

    
}



//微博首页--从网络工具哪里获取数据
extension YMListViewModel{
    
    func loadHomeData(isPullUp: Bool,isFinish:(isSuc:Bool,count:Int) ->()) {
        
        var maxId:Int64 = 0
        var sinceId:Int64 = 0
        
        if isPullUp {
            maxId = statusList.last?.status?.id ?? 0
            if maxId > 0 {
                maxId -= 1
            }
            
        }else{
            sinceId = statusList.first?.status?.id ?? 0
        }
        
        
        
      YMNetworkTools.sharedTools.loadHomeData(sinceId, maxId: maxId, success: { (response) in
        
        //1.解析数据
        guard let res = response?["statuses"] as? [[String:AnyObject]] else{
            isFinish(isSuc: false, count: 0)
            return
        }
        var tempArr:[YMStatusViewModel] = [YMStatusViewModel]()
        
        for dic in res{
            
            let status = YMStatus(dic: dic)
            
            let statusViewModel = YMStatusViewModel(status: status)
            
            tempArr.append(statusViewModel)
            
        }
        
        if isPullUp{
            self.statusList = self.statusList + tempArr
            //2.传回Vc
            isFinish(isSuc: true, count: -1)
        }else{
            self.statusList = tempArr + self.statusList
            //2.传回Vc
            isFinish(isSuc: true, count: tempArr.count)
        }
        
   
        
        }) { (error) in
            print("\(error)")
            isFinish(isSuc: false, count: 0)
        }
        
        
        
        
//    YMNetworkTools.sharedTools.loadHomeData({ (response) in
//        //1.解析数据
//        var tempArr:[YMStatusViewModel] = [YMStatusViewModel]()
//        guard let res = response?["statuses"] as? [[String:AnyObject]] else{
//            return
//        }
//        
//        for dic in res{
//            
//            let status = YMStatus(dic: dic)
//            
//            let statusViewModel = YMStatusViewModel(status: status)
//            
//            tempArr.append(statusViewModel)
//            
//        }
//        
//        //赋值阶段
//        self.statusList = tempArr
//        
//        //2.传回Vc
//        isFinish(isSuc: true)
//        
//        
//        }) { (error) in
//            print("\(error)")
//            isFinish(isSuc: false)
//        }
    }
    
}



















